package com.aos.eap.adapter;

import android.util.SparseBooleanArray;
import android.widget.CheckBox;

import com.aos.eap.R;
import com.aos.eap.bean.DefectBean;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

import androidx.annotation.Nullable;


public class BadCodeListAdapter extends BaseQuickAdapter<DefectBean, BaseViewHolder> {
    private SparseBooleanArray mCheckStates = new SparseBooleanArray();

    public BadCodeListAdapter(@Nullable List<DefectBean> data) {
        super(R.layout.adapter_badcode_list, data);
    }

    public void clearPos() {
        mCheckStates = new SparseBooleanArray();
    }

    public void setAll(boolean check) {
        for (int i = 0; i < getData().size(); i++) {
            mCheckStates.put(i, check);
            getData().get(i).setSelect(check);
        }
        notifyDataSetChanged();
    }

    @Override
    protected void convert(BaseViewHolder helper, final DefectBean item) {
        helper.setText(R.id.tvCodeId, item.getDefectCode());
        helper.setText(R.id.tvCodeDesc, item.getDefectDesc());
        helper.setText(R.id.tvCodeCount, item.getDefectCnt());
        CheckBox checkbox = helper.getView(R.id.checkbox);
        checkbox.setTag(helper.getAdapterPosition());
        checkbox.setChecked(mCheckStates.get(helper.getAdapterPosition(), false));
        checkbox.setOnClickListener(v -> {
            boolean isChecked = ((CheckBox) v).isChecked();
            item.setSelect(isChecked);
            int pos = (int) v.getTag();
            if (isChecked) {
                mCheckStates.put(pos, true);
            } else {
                mCheckStates.put(pos, false);
            }
        });
    }
}
